# 第一阶段：构建阶段
FROM node:20-alpine AS build

WORKDIR /app

# 设置 npm registry 并安装 pnpm
RUN apk add --no-cache curl && \
    npm config set registry https://registry.npmmirror.com/ && \
    npm install -g pnpm

# 复制 package.json 和 pnpm-lock.yaml 并安装依赖
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --prod=false # 确保安装所有依赖，包括开发依赖

# 复制源代码并执行构建
COPY . ./
RUN pnpm run build

# 第二阶段：生产阶段
FROM node:20-alpine

WORKDIR /app

# 创建一个非根用户
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

# 从构建阶段复制必要的文件
COPY --from=build /app/package.json ./
COPY --from=build /app/node_modules ./node_modules/
COPY --from=build /app/dist ./dist/
COPY --from=build /app/config ./config/

# 暴露应用程序将要监听的端口
EXPOSE 8088

# 启动命令
CMD ["node", "dist/main"]